<template>
  <div class="exchange-rate-converter-tool">
    <div class="tool-header">
      <h1>汇率转换器</h1>
      <p>实时汇率转换工具</p>
    </div>

    <div class="tool-container">
      <el-row :gutter="20">
        <!-- 转换器主体 -->
        <el-col :span="16">
          <div class="converter-section">
            <!-- 汇率转换 -->
            <div class="conversion-main">
              <h3>汇率转换</h3>
              <div class="conversion-inputs">
                <div class="input-group">
                  <label>金额：</label>
                  <el-input-number
                    v-model="amount"
                    :precision="2"
                    :step="0.01"
                    :min="0"
                    size="large"
                    placeholder="输入金额"
                    @change="convertCurrency"
                  />
                </div>
                <div class="input-group">
                  <label>从：</label>
                  <el-select v-model="fromCurrency" @change="convertCurrency" size="large">
                    <el-option
                      v-for="currency in currencies"
                      :key="currency.code"
                      :label="`${currency.code} - ${currency.name}`"
                      :value="currency.code"
                    />
                  </el-select>
                </div>
                <div class="conversion-arrow">
                  <el-icon :size="24"><Switch /></el-icon>
                  <el-button @click="swapCurrencies" type="primary" circle>
                    <el-icon><Refresh /></el-icon>
                  </el-button>
                </div>
                <div class="input-group">
                  <label>到：</label>
                  <el-select v-model="toCurrency" @change="convertCurrency" size="large">
                    <el-option
                      v-for="currency in currencies"
                      :key="currency.code"
                      :label="`${currency.code} - ${currency.name}`"
                      :value="currency.code"
                    />
                  </el-select>
                </div>
                <div class="input-group">
                  <label>结果：</label>
                  <el-input-number
                    v-model="convertedAmount"
                    :precision="2"
                    :step="0.01"
                    size="large"
                    readonly
                    placeholder="转换结果"
                  />
                </div>
              </div>
              
              <div class="conversion-info" v-if="conversionInfo">
                <div class="rate-info">
                  <span>汇率：1 {{ fromCurrency }} = {{ conversionInfo.rate }} {{ toCurrency }}</span>
                  <span class="update-time">更新时间：{{ conversionInfo.updateTime }}</span>
                </div>
              </div>
            </div>
            
            <!-- 实时汇率表 -->
            <div class="rate-table">
              <h3>实时汇率表</h3>
              <div class="table-container">
                <div class="table-header">
                  <div class="header-cell">货币</div>
                  <div class="header-cell">名称</div>
                  <div class="header-cell">汇率</div>
                  <div class="header-cell">变化</div>
                </div>
                <div 
                  v-for="rate in rateTable" 
                  :key="rate.code"
                  class="table-row"
                  @click="selectCurrency(rate.code)"
                >
                  <div class="cell currency-code">{{ rate.code }}</div>
                  <div class="cell currency-name">{{ rate.name }}</div>
                  <div class="cell rate-value">{{ rate.rate }}</div>
                  <div class="cell rate-change" :class="{ 'positive': rate.change > 0, 'negative': rate.change < 0 }">
                    {{ rate.change > 0 ? '+' : '' }}{{ rate.change }}%
                  </div>
                </div>
              </div>
            </div>
            
            <!-- 历史汇率 -->
            <div class="history-section">
              <h3>转换历史</h3>
              <div class="history-list">
                <div 
                  v-for="(item, index) in history" 
                  :key="index"
                  class="history-item"
                  @click="loadFromHistory(item)"
                >
                  <div class="history-conversion">
                    {{ item.amount }} {{ item.fromCurrency }} = {{ item.convertedAmount }} {{ item.toCurrency }}
                  </div>
                  <div class="history-rate">汇率：{{ item.rate }}</div>
                  <div class="history-time">{{ item.time }}</div>
                </div>
                <div v-if="history.length === 0" class="empty-history">
                  暂无转换历史
                </div>
              </div>
            </div>
          </div>
        </el-col>

        <!-- 工具和说明 -->
        <el-col :span="8">
          <div class="tools-section">
            <div class="section-header">
              <h3>常用货币</h3>
            </div>
            
            <div class="common-currencies">
              <div 
                v-for="currency in commonCurrencies" 
                :key="currency.code"
                class="currency-item"
                @click="setCommonCurrency(currency.code)"
              >
                <div class="currency-flag">{{ currency.flag }}</div>
                <div class="currency-info">
                  <div class="currency-code">{{ currency.code }}</div>
                  <div class="currency-name">{{ currency.name }}</div>
                </div>
                <div class="currency-rate">{{ currency.rate }}</div>
              </div>
            </div>
          </div>
          
          <div class="help-section">
            <el-card>
              <template #header>
                <div class="card-header">
                  <span>使用说明</span>
                </div>
              </template>
              
              <div class="help-content">
                <h4>支持货币：</h4>
                <ul>
                  <li><strong>主要货币：</strong> USD、EUR、GBP、JPY、CNY</li>
                  <li><strong>亚洲货币：</strong> HKD、SGD、KRW、THB、MYR</li>
                  <li><strong>其他货币：</strong> AUD、CAD、CHF、NZD</li>
                </ul>
                
                <h4>功能特点：</h4>
                <ul>
                  <li>实时汇率更新</li>
                  <li>双向转换</li>
                  <li>历史记录</li>
                  <li>常用货币快速选择</li>
                </ul>
                
                <h4>注意事项：</h4>
                <div class="warning">
                  <p>⚠️ 汇率仅供参考，实际以银行汇率为准</p>
                  <p>⚠️ 汇率实时变动，建议及时更新</p>
                  <p>⚠️ 大额交易请咨询专业金融机构</p>
                </div>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Switch, Refresh } from '@element-plus/icons-vue'

const amount = ref(100)
const fromCurrency = ref('USD')
const toCurrency = ref('CNY')
const convertedAmount = ref(0)
const history = ref([])

const currencies = [
  { code: 'USD', name: '美元', rate: 1.0000 },
  { code: 'CNY', name: '人民币', rate: 7.2500 },
  { code: 'EUR', name: '欧元', rate: 0.9200 },
  { code: 'GBP', name: '英镑', rate: 0.7900 },
  { code: 'JPY', name: '日元', rate: 150.0000 },
  { code: 'HKD', name: '港币', rate: 7.8000 },
  { code: 'SGD', name: '新加坡元', rate: 1.3500 },
  { code: 'KRW', name: '韩元', rate: 1300.0000 },
  { code: 'THB', name: '泰铢', rate: 35.0000 },
  { code: 'MYR', name: '马来西亚林吉特', rate: 4.5000 },
  { code: 'AUD', name: '澳元', rate: 1.5000 },
  { code: 'CAD', name: '加元', rate: 1.3500 },
  { code: 'CHF', name: '瑞士法郎', rate: 0.8800 },
  { code: 'NZD', name: '新西兰元', rate: 1.6000 }
]

const commonCurrencies = [
  { code: 'USD', name: '美元', flag: '🇺🇸', rate: '1.0000' },
  { code: 'CNY', name: '人民币', flag: '🇨🇳', rate: '7.2500' },
  { code: 'EUR', name: '欧元', flag: '🇪🇺', rate: '0.9200' },
  { code: 'GBP', name: '英镑', flag: '🇬🇧', rate: '0.7900' },
  { code: 'JPY', name: '日元', flag: '🇯🇵', rate: '150.0000' },
  { code: 'HKD', name: '港币', flag: '🇭🇰', rate: '7.8000' }
]

const rateTable = computed(() => {
  return currencies.map(currency => ({
    ...currency,
    change: (Math.random() - 0.5) * 2 // 模拟汇率变化
  }))
})

const conversionInfo = computed(() => {
  const fromRate = currencies.find(c => c.code === fromCurrency.value)?.rate || 1
  const toRate = currencies.find(c => c.code === toCurrency.value)?.rate || 1
  const rate = (toRate / fromRate).toFixed(4)
  
  return {
    rate: rate,
    updateTime: new Date().toLocaleString('zh-CN')
  }
})

const convertCurrency = () => {
  if (!amount.value || !fromCurrency.value || !toCurrency.value) return
  
  const fromRate = currencies.find(c => c.code === fromCurrency.value)?.rate || 1
  const toRate = currencies.find(c => c.code === toCurrency.value)?.rate || 1
  
  const result = (amount.value * toRate) / fromRate
  convertedAmount.value = parseFloat(result.toFixed(2))
  
  // 添加到历史
  addToHistory()
}

const swapCurrencies = () => {
  const temp = fromCurrency.value
  fromCurrency.value = toCurrency.value
  toCurrency.value = temp
  convertCurrency()
}

const selectCurrency = (currencyCode) => {
  if (fromCurrency.value === currencyCode) {
    toCurrency.value = currencyCode
  } else {
    fromCurrency.value = currencyCode
  }
  convertCurrency()
}

const setCommonCurrency = (currencyCode) => {
  fromCurrency.value = currencyCode
  convertCurrency()
  ElMessage.success(`已选择${currencyCode}`)
}

const addToHistory = () => {
  const historyItem = {
    amount: amount.value,
    convertedAmount: convertedAmount.value,
    fromCurrency: fromCurrency.value,
    toCurrency: toCurrency.value,
    rate: conversionInfo.value.rate,
    time: new Date().toLocaleString('zh-CN')
  }
  
  history.value.unshift(historyItem)
  
  // 限制历史记录数量
  if (history.value.length > 20) {
    history.value = history.value.slice(0, 20)
  }
}

const loadFromHistory = (item) => {
  amount.value = item.amount
  fromCurrency.value = item.fromCurrency
  toCurrency.value = item.toCurrency
  convertCurrency()
}

onMounted(() => {
  convertCurrency()
})
</script>

<style scoped>
.exchange-rate-converter-tool {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.tool-header {
  text-align: center;
  margin-bottom: 40px;
}

.tool-header h1 {
  font-size: 2.5rem;
  color: #333;
  margin-bottom: 10px;
}

.tool-header p {
  color: #666;
  font-size: 1.1rem;
}

.tool-container {
  margin-bottom: 40px;
}

.converter-section,
.tools-section {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border: 1px solid #f0f0f0;
  height: 100%;
}

.conversion-main,
.rate-table,
.history-section {
  margin-bottom: 30px;
}

.conversion-main h3,
.rate-table h3,
.history-section h3 {
  color: #333;
  margin-bottom: 20px;
  font-size: 1.3rem;
}

.conversion-inputs {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.input-group {
  display: flex;
  align-items: center;
  gap: 15px;
}

.input-group label {
  min-width: 60px;
  font-weight: 500;
  color: #666;
}

.input-group .el-input-number,
.input-group .el-select {
  flex: 1;
}

.conversion-arrow {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0;
  gap: 15px;
}

.conversion-arrow .el-icon {
  color: #667eea;
}

.conversion-info {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 15px;
  margin-top: 20px;
}

.rate-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.update-time {
  font-size: 0.9rem;
  color: #666;
}

.table-container {
  background: #f8f9fa;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #e9ecef;
}

.table-header {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr 1fr;
  background: #667eea;
  color: white;
  font-weight: bold;
}

.header-cell {
  padding: 12px;
  text-align: center;
  border-right: 1px solid rgba(255, 255, 255, 0.2);
}

.header-cell:last-child {
  border-right: none;
}

.table-row {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr 1fr;
  border-bottom: 1px solid #e9ecef;
  cursor: pointer;
  transition: all 0.2s ease;
}

.table-row:hover {
  background: #e9ecef;
}

.table-row:last-child {
  border-bottom: none;
}

.cell {
  padding: 12px;
  text-align: center;
  border-right: 1px solid #e9ecef;
  background: white;
}

.cell:last-child {
  border-right: none;
}

.currency-code {
  font-weight: bold;
  color: #667eea;
}

.currency-name {
  color: #666;
}

.rate-value {
  font-weight: bold;
  color: #333;
}

.rate-change.positive {
  color: #28a745;
}

.rate-change.negative {
  color: #dc3545;
}

.history-list {
  max-height: 300px;
  overflow-y: auto;
}

.history-item {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid #e9ecef;
}

.history-item:hover {
  background: #e9ecef;
  transform: translateY(-1px);
}

.history-conversion {
  font-size: 1rem;
  color: #333;
  margin-bottom: 5px;
  font-weight: 500;
}

.history-rate {
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 5px;
}

.history-time {
  font-size: 0.8rem;
  color: #999;
}

.empty-history {
  text-align: center;
  color: #999;
  padding: 40px 0;
  font-style: italic;
}

.section-header {
  margin-bottom: 20px;
}

.section-header h3 {
  color: #333;
  margin: 0;
  font-size: 1.3rem;
}

.common-currencies {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.currency-item {
  display: flex;
  align-items: center;
  gap: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  padding: 15px;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid #e9ecef;
}

.currency-item:hover {
  background: #e9ecef;
  transform: translateY(-2px);
}

.currency-flag {
  font-size: 1.5rem;
}

.currency-info {
  flex: 1;
}

.currency-code {
  font-size: 1rem;
  font-weight: bold;
  color: #333;
  margin-bottom: 2px;
}

.currency-name {
  font-size: 0.9rem;
  color: #666;
}

.currency-rate {
  font-size: 1rem;
  font-weight: bold;
  color: #667eea;
}

.help-section {
  margin-top: 20px;
}

.card-header {
  font-weight: bold;
  color: #333;
}

.help-content h4 {
  color: #333;
  margin: 20px 0 10px 0;
  font-size: 1.1rem;
}

.help-content ul {
  color: #666;
  padding-left: 20px;
  margin-bottom: 15px;
}

.help-content li {
  margin-bottom: 5px;
}

.warning {
  background: #fff3cd;
  padding: 15px;
  border-radius: 8px;
  border-left: 4px solid #ffc107;
  margin: 15px 0;
}

.warning p {
  margin: 5px 0;
  color: #856404;
}

@media (max-width: 768px) {
  .tool-header h1 {
    font-size: 2rem;
  }
  
  .input-group {
    flex-direction: column;
    align-items: stretch;
  }
  
  .input-group label {
    min-width: auto;
  }
  
  .table-header,
  .table-row {
    grid-template-columns: 1fr;
  }
  
  .header-cell,
  .cell {
    border-right: none;
    border-bottom: 1px solid #e9ecef;
  }
  
  .rate-info {
    flex-direction: column;
    gap: 10px;
  }
  
  .currency-item {
    flex-direction: column;
    text-align: center;
  }
}
</style>
